package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.BankCard;
import com.example.buildtable.pojo.Member;
import com.example.common.util.R;
import com.example.buildtable.pojo.BankTransfer;
import com.example.buildtable.service.BankTransferService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "银行汇款")
@RequestMapping(value = "/bankTransfer/")
public class BankTransferController {
    @Autowired
    private BankTransferService bankTransferService;
    
    @Operation(summary = "查询所有银行汇款数据")
    @GetMapping(value = "/getAllBankTransfer")
    public R getAllBankTransfer() {
        LambdaJoinQueryWrapper<BankTransfer> wrapper = new LambdaJoinQueryWrapper<>(BankTransfer.class);
        wrapper.innerJoin(BankCard.class, BankCard::getBackCardId, BankTransfer::getBankCardId)
                .innerJoin(Member.class, Member::getMemberId, BankTransfer::getBankTransferMemberId);
        List<BankTransfer> bankTransferList = bankTransferService.list(wrapper);
        return R.ok("查询所有银行汇款数据成功").data("bankTransferList", bankTransferList);
    }
    
    @Operation(summary = "增加银行汇款")
    @PostMapping(value = "/addBankTransfer")
    public R addBankTransfer(BankTransfer bankTransfer) {
        boolean flag = bankTransferService.save(bankTransfer);
        if (flag) {
            return R.ok("增加银行汇款成功");
        } else {
            return R.ok("增加银行汇款失败");
        }
    }

    @Operation(summary = "删除银行汇款")
    @GetMapping(value = "/deleteBankTransfer/{bankTransferId}")
    public R deleteBankTransfer(@PathVariable("bankTransferId") Integer bankTransferId) {
        boolean flag = bankTransferService.removeById(bankTransferId);
        if (flag) {
            return R.ok("删除银行汇款成功");
        } else {
            return R.ok("删除银行汇款失败");
        }
    }

    @Operation(summary = "按照银行汇款查看")
    @GetMapping(value = "/getBankTransferById/{bankTransferId}")
    public R getBankTransferById(@PathVariable("bankTransferId") Integer bankTransferId) {
        LambdaJoinQueryWrapper<BankTransfer> wrapper = new LambdaJoinQueryWrapper<>(BankTransfer.class);
        wrapper.innerJoin(BankCard.class, BankCard::getBackCardId, BankTransfer::getBankCardId)
                .innerJoin(Member.class, Member::getMemberId, BankTransfer::getBankTransferMemberId);
        wrapper.eq(BankTransfer::getBankTransferId, bankTransferId);
        BankTransfer bankTransfer = bankTransferService.getOne(wrapper);
        if (bankTransfer != null) {
            return R.ok("按照银行汇款查看成功").data("bankTransfer", bankTransfer);
        } else {
            return R.error("按照银行汇款查看失败");
        }
    }

    @Operation(summary = "修改银行汇款")
    @PostMapping(value = "/updateBankTransfer")
    public R updateBankTransfer(BankTransfer bankTransfer) {
        boolean flag = bankTransferService.updateById(bankTransfer);
        if (flag) {
            return R.ok("修改银行汇款成功");
        } else {
            return R.ok("修改银行汇款失败");
        }
    }
}